/**
 * 
 */
package com.swe727.spring2011.constraint1.observation;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import com.swe727.spring2011.constraint1.coordinate.Coordinate;
import com.swe727.spring2011.constraint1.datastore.DataStore;

/**
 * An ObservationDataStore stores observations in a collection
 * accessible by coordinate.
 * @author Trevor Wright
 *
 */
public class ObservationDataStore implements DataStore<Observation, Coordinate> {
	
	/**
	 * Auto-generated serial ID
	 */
	private static final long serialVersionUID = 1663143122879080426L;
	/**
	 * This data store's observations.
	 */
	private HashMap<Coordinate, HashSet<Observation>> observations;
	/**
	 * Singleton pattern
	 */
	private static ObservationDataStore store;
	
	public synchronized static ObservationDataStore getStore() {
		if (store == null) {
			store = new ObservationDataStore();
		}
		
		return store;
	}
	/**
	 * Create a new data store for observations.
	 */
	private ObservationDataStore() {
		observations = new HashMap<Coordinate, HashSet<Observation>>();
	}
	
	/* (non-Javadoc)
	 * @see com.swe727.spring2011.constraint1.datastore.DataStore#add(com.swe727.spring2011.constraint1.message.Message)
	 */
	@Override
	public synchronized void add(Observation data) {
		Coordinate coord = data.getCoordinate();
		HashSet<Observation> obs = observations.get(coord);
		
		if (obs == null) {
			obs = new HashSet<Observation>();
			observations.put(coord, obs);
		}
		
		obs.add(data);
	}

	/* (non-Javadoc)
	 * @see com.swe727.spring2011.constraint1.datastore.DataStore#getAll()
	 */
	@Override
	public synchronized HashSet<Observation> getAll(Coordinate coordinate) {
		HashSet<Observation> obs = observations.get(coordinate);
		
		return obs != null ? obs : new HashSet<Observation>();
	}
}
